package com.fiwizone.ultimateshoppinglist;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class UltimateShoppingList extends ListActivity {
	private static final int ACTIVITY_CREATE=0;
    private static final int ACTIVITY_EDIT=1;
    
	// Menu item ids
    public static final int MENU_ITEM_DELETE = Menu.FIRST;
    public static final int MENU_ITEM_INSERT = Menu.FIRST + 1;
    public static final int MENU_ITEM_EDIT = Menu.FIRST + 2;
    
	private ShoppingData mDbHelper;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new ShoppingData(this);
        mDbHelper.open();
        
        setContentView(R.layout.main);
        fillData();      
        // Inform the list we provide context menus for items
        registerForContextMenu(getListView());
    }    
    
    private void fillData() {
        // Get all of the rows from the database and create the item list
    	Cursor cursor = mDbHelper.fetchAllLists();
        startManagingCursor(cursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{ShoppingData.KEY_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{android.R.id.text1};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter lists = 
        	    new SimpleCursorAdapter(this, R.layout.main_item, cursor, from, to);
        setListAdapter(lists);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // This is our one standard application action -- inserting a
        // new note into the list.
        menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert_list)
                .setIcon(android.R.drawable.ic_menu_add);

        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case MENU_ITEM_INSERT:
        	createList();
            return true;         
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
    
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);        
        menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_edit_list);
	}

    @Override
	public boolean onContextItemSelected(MenuItem item) {
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	
		switch(item.getItemId()) {
    	case MENU_ITEM_DELETE:    		
	        mDbHelper.deleteList(info.id);
	        fillData();
	        return true;
    	case MENU_ITEM_EDIT:
    		Intent i = new Intent(this, ListEdit.class);
            i.putExtra(ShoppingData.KEY_ROWID, info.id);
            startActivityForResult(i, ACTIVITY_EDIT);
            return true;     
		}
		
		return super.onContextItemSelected(item);
	}
    
    private void createList() {
        Intent i = new Intent(this, ListEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, ItemList.class);
        i.putExtra(ShoppingData.KEY_LISTID, id);
        startActivity(i);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
}